package com.effectiv.gooruda.rest.api.service;

import java.util.Date;
import java.util.List;

import jodd.util.StringUtil;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.mongodb.core.CollectionCallback;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import com.effectiv.gooruda.rest.api.domain.Log;
import com.effectiv.gooruda.rest.api.exception.IllegalLogTypeException;
import com.effectiv.gooruda.rest.api.operation.LogOperations;
import com.effectiv.gooruda.security.rest.RESTSecurityUtil;
import com.mongodb.DBCollection;
import com.mongodb.MongoException;

@Slf4j
@Component
@Deprecated
public class LogServiceImpl implements LogOperations {

	private final String LOG_COLLECTION_BASE = "c_log_";

	private final String ALLOWED_LOG_TYPES[] = { "DEBUG", "INFO", "WARN",
			"ERROR", "FATAL" };

	@Autowired
	private MongoTemplate template;
	
	@Deprecated
	protected void validateLogType(String type) {
		if (StringUtil.equalsOne(type, ALLOWED_LOG_TYPES) == -1) {
			throw new IllegalLogTypeException(
					"Log type - "
							+ type
							+ " is not supported. Allowed values - DEBUG, INFO, WARN,ERROR, FATAL.");
		}
	}
	
	@Deprecated
	public Log save(Log l) {

		String type = l.getType();
		validateLogType(type);
		template.save(l, LOG_COLLECTION_BASE + RESTSecurityUtil.getAppId());
		
		
		
		return l;
	}
	
	@Deprecated
	public List<Log> findByType(String type, int pageNo, int pageSize) {
		validateLogType(type);
		Query query = new Query();
		query.addCriteria(Criteria.where("type").is(type));
		query.skip(pageNo * pageSize);
		query.limit(pageSize);

		return template.find(query, Log.class, LOG_COLLECTION_BASE
				+ RESTSecurityUtil.getAppId());
	}
	
	@Deprecated
	public List<Log> findBetween(Date start, Date end, int pageNo, int pageSize) {

		log.info("Running query");

		Query query = new Query();
		query.addCriteria(Criteria.where("createdDate").gte(start).lt(end));
		query.skip(pageNo * pageSize);
		query.limit(pageSize);
		return template.find(query, Log.class, LOG_COLLECTION_BASE
				+ RESTSecurityUtil.getAppId());
	}
	
	@Deprecated
	public List<Log> findByLogText(String text, int pageNo, int pageSize) {
		Query query = new Query();

		query.addCriteria(Criteria.where("text").regex(text));
		query.skip(pageNo * pageSize);
		query.limit(pageSize);
		return template.find(query, Log.class, LOG_COLLECTION_BASE
				+ RESTSecurityUtil.getAppId());

	}
	
	@Deprecated
	public long countByDateRange(Date start, Date end) {
		Query query = new Query();
		query.addCriteria(Criteria.where("createdDate").gte(start).lt(end));
		return countDocuments(
				LOG_COLLECTION_BASE + RESTSecurityUtil.getAppId(), query);
	}
	
	@Deprecated
	public long countByType(String type) {
		validateLogType(type);
		Query query = new Query();
		query.addCriteria(Criteria.where("type").is(type));

		return countDocuments(
				LOG_COLLECTION_BASE + RESTSecurityUtil.getAppId(), query);
	}
	
	@Deprecated
	public long countByText(String text) {
		Query query = new Query();
		query.addCriteria(Criteria.where("text").regex(text));

		return countDocuments(
				LOG_COLLECTION_BASE + RESTSecurityUtil.getAppId(), query);
	}
	
	@Deprecated
	private long countDocuments(final String collection, final Query query) {
		return template.execute(collection, new CollectionCallback<Long>() {

			public Long doInCollection(DBCollection collection)
					throws MongoException, DataAccessException {
				return collection.count(query.getQueryObject());
			}
		});
	}

}
